package com.skirmisher.inspection.daily.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.skirmisher.inspection.common.utils.PageUtils;
import com.skirmisher.inspection.daily.entity.InsDailyTempSave;
import com.skirmisher.inspection.daily.entity.InsDailyTemplate;
import com.skirmisher.inspection.daily.entity.InsDaliyTempQueryEntity;
import com.skirmisher.inspection.daily.service.InsDaliyTemplateService;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName InsDailyTemplateController
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/10 下午9:57
 * @Version 1.0
 */
@Api(tags = "日常巡查模板接口")
@RestController
@RequestMapping("/daily")
public class InsDailyTemplateController {

    @Autowired
    private InsDaliyTemplateService insDaliyTemplateService;


    @ApiOperation(value = "保存日常巡查模板")
    //@ApiOperationSupport(ignoreParameters = {"name"})
    @PostMapping(value = "/saveDailTemp")
    public ResponseModel saveDailTemp(@RequestBody @Validated InsDailyTempSave tempSave){

        ResponseModel responseModel = new ResponseModel();

        insDaliyTemplateService.saveDailTemp(tempSave,responseModel);
        return  responseModel;
    }

    @ApiOperation(value = "获取巡查模板详情")
    @ApiImplicitParam(name = "tempId",value = "模板id",required = true)
    @GetMapping(value = "/getTempDetail")
    public ResponseModel getTempDetail(String tempId){
        ResponseModel responseModel = new ResponseModel();
        if (StringUtils.isEmpty(tempId)){
            responseModel.setCode(201);
            responseModel.setMessage("无模板id");
            return responseModel;
        }
        InsDailyTemplate insDailyTemplate = insDaliyTemplateService.getTempDetail(tempId);
        responseModel.setData(insDailyTemplate);
        return  responseModel;
    }
    @ApiOperation(value = "巡查模板页面查询")
    @GetMapping(value = "/getTempPageData")
    public ResponseModel getTempPageData(InsDaliyTempQueryEntity query){

        ResponseModel responseModel = new ResponseModel();
        //参数校验
        Integer startRow = PageUtils.getStartRow(query.getPage(), query.getRows());
        query.setStartRows(startRow);
        if (startRow == null || startRow < 0){
            responseModel.setCode(201);
            responseModel.setMessage("分页参数有误");
            return  responseModel;
        }

        insDaliyTemplateService.getTempPageData(query);
        responseModel.setDataDataGrid(query.getDateList(), query.getCount());


        return responseModel;
    }


    /**
     * @Author hanwangxin
     * @Description //TODO 批量删除巡检模板 * @Date 下午9:55 2020/12/12
     * @Param [tempIds]
     * @return com.skirmisher.inspection.inspection.testController.ResponseModel
     **/
    @ApiOperation(value = "批量删除模板")
    @ApiImplicitParam(name = "tempIds",value = "模板id集合",required = true)
    @PostMapping(value = "/deletDaliyTemp")
    public ResponseModel deletDaliyTemp(@RequestBody List<String> tempIds){
        ResponseModel responseModel = new ResponseModel();
        insDaliyTemplateService.deletDaliyTemp(tempIds);
        return responseModel;
    }

    @ApiOperation(value = "更新巡查模板")
    //@ApiOperationSupport(ignoreParameters = {"name"})
    @PostMapping(value = "/updateDaliyTemp")
    public ResponseModel updateDaliyTemp(@RequestBody @Validated InsDailyTempSave tempSave){

        ResponseModel responseModel = new ResponseModel();

        String id = tempSave.getId();
        if (StringUtils.isEmpty(id)){
            responseModel.setCode(201);
            responseModel.setMessage("无模板id");
            return responseModel;
        }

        insDaliyTemplateService.updateDaliyTemp(tempSave,responseModel);
        return  responseModel;

    }

    /**
     * @Author hanwangxin
     * @Description //TODO 批量启用巡查模板 * @Date 下午9:55 2020/12/12
     * @Param [tempIds]
     * @return com.skirmisher.inspection.inspection.testController.ResponseModel
     **/
    @ApiOperation(value = "批量启用巡查模板")
    @ApiImplicitParam(name = "tempIds",value = "模板id集合",required = true)
    @PostMapping(value = "/startDaliyTemp")
    public ResponseModel startDaliyTemp(@RequestBody List<String> tempIds){
        ResponseModel responseModel = new ResponseModel();
        insDaliyTemplateService.startDaliyTemp(tempIds);
        return responseModel;
    }
}
